<template>
	<view>
		<scroll-view scroll-y="true">
			<view class="content">
				<text class="title">{{detailList.title}}</text>
				<view class="read">
					<text>{{detailList.readNum}}阅读</text>
					<text>|</text>
					<text>发布于{{detailList.publishDate}}</text>
				</view>
				<view class="cover">
					<image :src="baseUrl + detailList.cover" mode=""></image>
				</view>
				<!-- <text class="content">{{detailList.content}}</text> -->
				<rich-text :nodes="detailList.content" class="content"></rich-text>
				<view class="thumb">
					<u-icon name="thumb-up" size="40"></u-icon>
					<text>{{detailList.likeNum}}</text>
				</view>
				<view class="recommend">
					<text class="title">
						新闻推荐
					</text>
					<view class="recommend-item" v-for="item in recommendList">
						<text class="name">{{item.title}}</text>
						<text class="desc">{{item.content}}</text>
						<image :src="baseUrl+ item.cover" mode=""></image>
						<view class="buttom">
							<view class="buttom-item">
								<u-icon name="chat" color="#666"></u-icon>
								<text>{{item.commentNum}}</text>
							</view>
							<view class="buttom-item">
								<u-icon name="thumb-up" color="#666"></u-icon>
								<text>{{item.likeNum}}</text>
							</view>
							<view class="buttom-item">
								<u-icon name="eye" color="#666"></u-icon>
								<text class="eye">{{item.readNum}}</text>
							</view>
							<view class="buttom-item">
								<text>{{item.publishDate}}</text>
							</view>
						</view>
					</view>
				</view>
				<view class="review" v-if="flag">
					<view class="total">
						{{commentTotal}}条评论
					</view>
					<view class="review-item" v-for="item in comment">
						<view class="left">
							<view class="avatar">
								<u-icon name="account-fill" label-color="#fff"></u-icon>
							</view>
						</view>
						<view class="right">
							<view class="name">{{item.userName}}</view>
							<text class="content">{{item.content}}</text>
							<view class="bottom">
								<view class="thumb">
									<u-icon name="thumb-up"></u-icon>
									<text>{{item.likeNum}}</text>
								</view>
								<text class="date">{{item.commentDate}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="self-review">
			<view class="input" @click="show = true">请输入评论内容</view>
			<view class="other">
				<view class="other-item">
					<u-icon name="chat" size="45"></u-icon>
					<text>{{detailList.commentNum}}</text>
				</view>
				<view class="other-item">
					<u-icon name="thumb-up" size="45"></u-icon>
					<text>{{detailList.likeNum}}</text>
				</view>
				<view class="other-item">
					<u-icon name="eye" size="45"></u-icon>
					<text>{{detailList.readNum}}</text>
				</view>
			</view>
		</view>
		<u-popup v-model="show" mode="bottom" duration="180">
			<view class="field">
				<view class="left">
					<!-- <u-input class="input" type="textarea" placeholder="请输入内容" v-model="value" @input="changeValue"></u-input> -->
					<textarea class="input" placeholder="请输入内容" auto-height="true" v-model="value" @input="changeValue"></textarea>
				</view>
				<button @click="express" :disabled="disabled" :class="disabled === true ? 'dis' : ''">发送</button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				detailList: [],
				baseUrl: 'http://124.93.196.45:10001/',
				recommendList: [],
				commentList: [],
				commentTotal: 0,
				show: false,
				value: '',
				disabled: true,
				id: 0,
				flag: false
			};
		},
		onLoad(option) {
			this.id = option.id
			this.getDetailData(option.id)
			this.getRecommendData()
			this.getComment(option.id)
			uni.setNavigationBarTitle({
				title: option.title
			})
		},
		methods: {
			async getDetailData(id) {
				const res = await this.$http({
					url: `/prod-api/press/press/${id}`
				})
				console.log(res)
				this.detailList = res.data.data
				uni.setNavigationBarTitle({
					title: this.detailList.title
				})
			},
			async getRecommendData() {
				const res = await this.$http({
					url: '/prod-api/press/press/list',
					data: {
						top: 'Y'
					}
				})
				console.log(res)
				this.recommendList = res.data.rows
				
			},
			async getComment(id) {
				const res = await this.$http({
					url: `/prod-api/press/comments/list?newsId=${id}`,
				})
				console.log(res)
				this.commentTotal = res.data.total
				this.commentList = res.data.rows
				this.flag = true
			},
			changeValue(){
				if(this.value !== ''){
					this.disabled = false
				}else{
					this.disabled = true
				}
			},
			express(){
				console.log(this.value)
				uni.request({
					url: this.$baseurl + '/prod-api/press/pressComment',
					method: 'POST',
					data: {
						newsId: this.id,
						content: this.value
					},
					header: {Authorization: uni.getStorageSync('token')},
					success: (res) => {
						console.log(res.data)
						this.show = false
					}
				})
			}
		},
		computed: {
			comment(){
				const res =  this.commentList.sort((a,b) =>{
					return new Date(b.commentDate).getTime() - new Date(a.commentDate).getTime()
				})
				return res.splice(0,20)
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		height: 100%;

		view {
			background-color: #F7F7F7;
			height: 100%;
			display: flex;
			flex-direction: column;

			scroll-view {
				height: 1129rpx;
				overflow: hidden;

				.content {
					height: fit-content;

					.title {
						background-color: #fff;
						font-size: 36rpx;
						line-height: 1.8em;
						font-weight: bold;
						padding: 20rpx;
					}

					.read {
						padding: 15rpx 20rpx;
						font-size: 22rpx;
						color: #666;
						background-color: #fff;
						display: flex;
						flex-direction: row;
						justify-content: start;
						align-items: center;

						text {
							margin-left: 20rpx;

							&:first-child {
								margin-left: 0;
							}
						}
					}

					.cover {
						background-color: #fff;
						display: flex;
						flex-direction: row;
						justify-content: center;
						padding: 20rpx;

						image {
							width: 100%;
						}
					}

					.content {
						background-color: #fff;
						padding: 20rpx;
						color: #333;
						line-height: 1.8em;
					}

					.thumb {
						background-color: #fff;
						display: flex;
						flex-direction: row;
						font-size: 35rpx;
						padding-left: 600rpx;
						color: #666;

						text {
							margin-left: 20rpx;
							font-size: 28rpx;
						}
					}

					.recommend {
						margin-top: 20rpx;
						background-color: #fff;
						height: fit-content;

						.recommend-item {
							display: flex;
							flex-direction: column;
							height: fit-content;
							padding: 20rpx;
							background-color: #fff;
							// border-bottom: 2rpx solid #eee;
							position: relative;

							&::after {
								content: '';
								position: absolute;
								width: 90%;
								height: 3rpx;
								bottom: 0;
								left: 30rpx;
								background-color: #eee;
							}

							.name {
								font-size: 32rpx;
								font-weight: bold;
								text-indent: 2em;
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
							}

							.desc {
								display: -webkit-box;
								-webkit-box-orient: vertical;
								-webkit-line-clamp: 2;
								overflow: hidden;
								margin-top: 20rpx;
								line-height: 1.5em;
							}

							image {
								border-radius: 20rpx;
								width: 100%;
								height: 320rpx;
								margin-top: 20rpx;
							}

							.buttom {
								display: flex;
								flex-direction: row;
								justify-content: start;
								margin-top: 20rpx;
								background-color: #fff;

								.buttom-item {
									margin-left: 20rpx;
									display: flex;
									flex-direction: row;
									align-items: center;
									font-size: 32rpx;
									background-color: #fff;

									&:first-child {
										margin-left: 0;
									}

									u-icon {
										background-color: #fff;
										color: #fff;
									}

									text {
										font-size: 22rpx;
										color: #666;
										background-color: #fff;
										margin-left: 5rpx;
									}
								}
							}
						}
					}

					.review {
						display: flex;
						flex-direction: column;
						background-color: #fff;
						margin-top: 20rpx;
						padding: 20rpx;

						.total {
							font-weight: bold;
							font-size: 35rpx;
							background-color: #fff;
						}

						.review-item {
							display: flex;
							flex-direction: row;
							justify-content: start;
							margin-top: 20rpx;
							box-sizing: border-box;
							width: 100%;
							background-color: #fff;
							padding-bottom: 20rpx;
							border-bottom: 2rpx solid #F7F7F7;

							.left {
								background-color: #fff;

								.avatar {
									background-color: #fff;
								}
							}

							.right {
								.name {
									background-color: #fff;
									// margin-left: 20rpx;
									padding-left: 20rpx;
									margin-top: -5rpx;
								}

								.bottom {
									background-color: #fff;

									.date {
										padding-left: 400rpx;
									}
								}
							}
						}
					}
				}
			}

			.self-review {
				border-top: 2rpx solid #f2f2f2;
				height: 100rpx;
				background-color: #fff;
				display: flex;
				flex-direction: row;
				padding: 20rpx;
				padding-top: 10rpx;
				padding-bottom: 30rpx;

				.input {
					width: 375rpx;
					border-radius: 10rpx;
					background-color: #f2f2f2;
					color: #909399;
					text-indent: 1em;
					display: flex;
					justify-content: center;
				}

				.other {
					background-color: #fff;
					display: flex;
					flex-direction: row;
					flex: 1;
					justify-content: space-around;

					.other-item {
						display: flex;
						flex-direction: column;
						background-color: #fff;
						font-size: 22rpx;
						margin-left: 20rpx;
						color: #909399;

						.u-icon {
							background-color: #fff;
						}
					}
				}
			}

			.u-drawer {
				opacity: 1;
				background-color: transparent;
				.field {
					padding: 20rpx;
					display: flex;
					flex-direction: row;
					justify-content: space-between;
					background-color: #fff;
						.input {
							width: 500rpx;
							padding: 20rpx;
							min-height: 80rpx;
							//border-radius: 15rpx;
							background-color: #f2f2f2;
							// background-color: transparent;
							display: flex;
							flex-direction: row;
							align-items: center;
							// background-color: transparent;
							// width: 500rpx;
						}
					button{
						width: 120rpx;
						height: 80rpx;
						font-size: 28rpx;
						background-color: #2b85e4;
						color: white;
						display: flex;
						flex-direction: row;
						align-items: center;
						justify-content: center;
					}
					.dis{
						background-color: #c8c9cc;
					}
				}
			}
		}
	}
</style>